package com.xcity.game.activity2.platform.qunhei;

import com.xcity.db.entity.Activity;
import com.xcity.db.entity.Player;
import com.xcity.game.account.AccountChannel;
import com.xcity.game.activity2.ActivityImpl;
import com.xcity.game.activity2.platform.IPacketHandler;
import com.xcity.game.activity2.platform.PlatformGift;
import com.xcity.game.activity2.platform.PlatformGiftDispatchService;
import com.xcity.game.common.Cause;
import com.xcity.game.common.IRegistry;
import com.xcity.game.common.MessageConst;
import com.xcity.game.common.PlayerPool;
import com.xcity.pomelo.push.msg.PlatformGiftPush;
import com.xcity.pomelo.request.PlatformGiftInfoRequest;
import com.xcity.pomelo.request.PlatformGiftRequest;
import com.xcity.pomelo.response.HintResponse;
import com.xcity.pomelo.response.PlatformGiftInfoResponse;

import naga.x.App;
import naga.x.net.Session;
import naga.x.service.ServiceException;

/**
 * 群黑游戏礼包（分享礼包）
 * @author yang.li
 *
 */
public class QunHeiGiftActivity extends ActivityImpl implements IPacketHandler {

	private IRegistry<AccountChannel, IPacketHandler> registry;
	private PlatformGift gift;
	
	public QunHeiGiftActivity(Activity entity) {
		super(entity);
		this.registry = App.getApp().getServiceManager().get(PlatformGiftDispatchService.class);
	}

	@Override
	public String getId() {
		return getClass().getName();
	}

	@Override
	public void init() {
		gift = entity.getDataObj(PlatformGift.class);
	}

	@Override
	public void save() {
	}

	@Override
	public void startup() throws ServiceException {
		registry.register(AccountChannel.QUNHEI, this);
	}

	@Override
	public void shutdown() throws ServiceException {
		registry.unRegister(AccountChannel.QUNHEI);
	}

	@Override
	public boolean update() {
		return true;
	}

	@Override
	public void setDirty() {
		init();
	}
	
	@Override
	public void requestGiftInfo(Session session, PlatformGiftInfoRequest req) {
		PlatformGift gift = this.gift;
		if (gift == null) {
			session.send(new HintResponse(req.getSerial(), MessageConst.HAVE_NO_REWARD));
			return;
		}
		Player player = session.getClient();
		session.send(new PlatformGiftInfoResponse(req.getSerial(), gift, getState(player, gift)));
	}
	
	@Override
	public void requestGift(Session session, PlatformGiftRequest req) {
		PlatformGift gift = this.gift;
		if (gift == null) {
			session.send(new HintResponse(req.getSerial(), MessageConst.HAVE_NO_REWARD));
			return;
		}
		Player player = session.getClient();
		byte state = getState(player, gift);
//		if (state == PlatformGift.STATE_CAN_NOT_RECEIVE) {
//			session.send(new HintResponse(req.getSerial(), MessageConst.EXCHANGE_CAN_NOT_RECEIVE));
//			return;
//		}
//		if (state == PlatformGift.STATE_RECEIVED) {
//			session.send(new HintResponse(req.getSerial(), MessageConst.EXCHANGE_RECEIVED));
//			return;
//		}
		if (state != PlatformGift.STATE_CAN_RECEIVE) {
			session.send(new PlatformGiftInfoResponse(req.getSerial(), gift, state));
			return;
		}
		receive(player, gift);
//		session.send(new Response(req.getSerial()));
		session.send(new PlatformGiftInfoResponse(req.getSerial(), gift, PlatformGift.STATE_RECEIVE_SUCC));
	}

	private void receive(Player player, PlatformGift gift) {
		player.poolSet(getPoolKey(gift), gift.id);
		gift.content.receive(player, Cause.QUNHEI_GIFT, false);
		player.send(new PlatformGiftPush(gift, PlatformGift.STATE_RECEIVED));
	}
	
	private static byte getState(Player player, PlatformGift gift) {
		String poolKey = getPoolKey(gift);
		int received = player.getPool().getInt(poolKey, PlatformGift.STATE_CAN_RECEIVE);
		if (received == gift.id) {
			return PlatformGift.STATE_RECEIVED;
		}
		return PlatformGift.STATE_CAN_RECEIVE;
	}
	
	private static String getPoolKey(PlatformGift gift) {
		return PlayerPool.PROPERTY_QUNHEI_GIFT_PREFIX + gift.type;
	}
	
}
